/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.lane.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.thinkgem.jeesite.modules.area_info.entity.AreaInfo;
import com.thinkgem.jeesite.modules.area_info.service.AreaInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.lane.entity.Lane;
import com.thinkgem.jeesite.modules.lane.service.LaneService;

import java.util.ArrayList;
import java.util.List;

/**
 * 车道信息Controller
 * @author xlp
 * @version 2019-04-12
 */
@Controller
@RequestMapping(value = "${adminPath}/lane/lane")
public class LaneController extends BaseController {

	@Autowired
	private LaneService laneService;

	@Autowired
	private AreaInfoService areaInfoService;
	
	@ModelAttribute
	public Lane get(@RequestParam(required=false) String id) {
		Lane entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = laneService.get(id);
		}
		if (entity == null){
			entity = new Lane();
		}
		return entity;
	}
	
	@RequiresPermissions("lane:lane:view")
	@RequestMapping(value = {"list", ""})
	public String list(Lane lane, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Lane> page = laneService.findPage(new Page<Lane>(request, response), lane);
		List<Lane> list = new ArrayList<Lane>();
		for(Lane l : page.getList()){
			l.setAreaName(areaInfoService.get(l.getAreaId()).getAreaName());
			list.add(l);
		}
		page.setList(list);
		model.addAttribute("page", page);
		return "modules/lane/laneList";
	}

	@RequiresPermissions("lane:lane:view")
	@RequestMapping(value = "form")
	public String form(Lane lane, Model model) {
		model.addAttribute("lane", lane);
		return "modules/lane/laneForm";
	}

	@RequiresPermissions("lane:lane:edit")
	@RequestMapping(value = "save")
	public String save(Lane lane, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, lane)){
			return form(lane, model);
		}
		laneService.save(lane);
		addMessage(redirectAttributes, "保存车道信息成功");
		return "redirect:"+Global.getAdminPath()+"/lane/lane/?repage";
	}
	
	@RequiresPermissions("lane:lane:edit")
	@RequestMapping(value = "delete")
	public String delete(Lane lane, RedirectAttributes redirectAttributes) {
		laneService.delete(lane);
		addMessage(redirectAttributes, "删除车道信息成功");
		return "redirect:"+Global.getAdminPath()+"/lane/lane/?repage";
	}

	@RequiresPermissions("lane:lane:view")
	@RequestMapping(value = "getAreaList")
	@ResponseBody
	public List<AreaInfo> getAreaList(HttpServletRequest request, HttpServletResponse response,
									   Model model) {
		List<AreaInfo> areaInfoList = areaInfoService.findList(new AreaInfo());
		model.addAttribute("areaList", areaInfoList);
		if (CollectionUtils.isEmpty(areaInfoList)){
			return new ArrayList<AreaInfo>();
		}else {
			return areaInfoList;
		}
	}

	/**
	 * 返回该区域车道列表
	 * @return
	 */
	@RequestMapping(value = "getLaneList")
	@ResponseBody
	public List<Lane> getLaneList(HttpServletRequest request){
		String areaid = request.getParameter("areaid");
		Lane lane = new Lane();
		lane.setAreaId(areaid);
		List<Lane> laneList = laneService.findList(lane);
		return laneList;
	}

}